package com.ego.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ego.product.dao.ProductAttrValueDao;
import com.ego.product.po.ProductAttrValue;
import com.ego.product.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author KamTang
 * @program: ego
 * @description spu属性值对应实现类
 * @date 2022-11-13 12:56:14
 */
@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValue> implements ProductAttrValueService {
    @Override
    public List<ProductAttrValue> get(Long spuId) {
        return this.baseMapper.selectList(new QueryWrapper<ProductAttrValue>().eq("spu_id", spuId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Long spuId, List<ProductAttrValue> productAttrValues) {
        // 删除这个spuId之前对应的所有
        this.baseMapper.delete(new QueryWrapper<ProductAttrValue>().eq("spu_id", spuId));
        List<ProductAttrValue> productAttrValuePos = productAttrValues.stream()
                .peek(productAttrValue -> productAttrValue.setSpuId(spuId)).collect(Collectors.toList());
        this.saveBatch(productAttrValuePos);
    }
}
